// pages/play/play.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    res: null,
    list: [],
    checked: [],
    order_number: [],
    refreshCard: null,
    refreshChecked: null,
    refresh: null,
    cardModal: null,
    index:'',
    mission:[],
    isShow:true,
    wanjia:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      res: options.resData
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      list: JSON.parse(this.data.res)
    })
    let that = this
    that.data.refreshChecked = setInterval(function () {
      for (var i in that.data.list) {
        that.data.checked[i] = that.data.list[i].checked
      }
      // console.log(that.data.checked)
      if (that.data.checked.includes(false)) {
        // console.log(that.data.checked.includes(false))
        that.data.refreshCard = function () {
          wx.request({
            url: app.globalData.baseUrl + '/refresh_referee_poker',
            method: "post",
            success: function (res) {
              that.setData({
                plateList: res.data
              })
            }
          })
        }
      } else {
        clearInterval(that.data.refreshCard)
        clearInterval(that.data.refresh)
        clearInterval(that.data.refreshChecked)
      }
    }, 1000)
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  next: function () {
    // this.Modal.showModal();
    // this.data.cardModal.showModal();
  },
  //返回
  back: function () {
    wx.navigateBack({
      delta: 3
    })
  },
  // 玩家选择身份
  identity:function(e ){
    
    let that = this
    // 获取当前checked的值存到this.data.checked
    for (var i in that.data.list) {
      that.data.checked[i] = that.data.list[i].checked
    }
    // 获取当前order_number的值存到this.data.order_number
      for (var i in that.data.list) {
        that.data.order_number[i] = that.data.list[i].order_number
      }
      let order_name = this.data.order_name
      // 获取当前点击下标
      that.data.index = e.currentTarget.dataset.key
      let index = that.data.index
      // console.log(index)
      // 当前选择身份的编号
      let data = this.data.order_number[index]
      console.log(that.data.checked)
      console.log(index)
      if(that.data.checked[index]==false){
        wx.request({
          url: 'http://192.168.0.104:8080/player_checked',
          method: 'post',
          data,
          success: function (res) {
            console.log(res)
            if (res.data == "") {
              wx.showToast({
                title: '已经被选中'
              })
            } else {
              let wanjia = res.data
              that.setData({
                isShow: !that.data.isShow,
                wanjia: wanjia
              })
            }
          }
        })
      }
      else{
        wx.showToast({
          title: '已经被选中'
        })
      }
      
    }
})